package
{
	import SEApp.se_app;
	import SEApp.se_sapp;
	
	import UIFree.controls.uif_list;
	import UIFree.uif_styles;
	
	import api.clt_okserver;
	import api.com.odnoklassniki.Odnoklassniki;
	import api.com.odnoklassniki.events.ApiCallbackEvent;
	import api.com.odnoklassniki.events.ApiServerEvent;
	import api.com.odnoklassniki.sdk.discussions.Discussions;
	import api.com.odnoklassniki.sdk.photos.Photos;
	
	import api_servers.api_server;
	
	import classes.clt_styles;
	
	import components.clt_list;
	
	import flash.display.LoaderInfo;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.system.Security;
	
	public class duscussion_ok extends se_sapp {
		public static const SECRETS: Object = {
			'27436800': '9B8100B59CE3BF8D85AE464D',
			'6278400': '0C8FBEF24707763ECF000635',
			'CBAPNNABABABABABA': '8DCE8C8EE7F030A9E15F16D1',
			'34205952': '776438E5F15FA66D2912F3C2' // dev
		}
			
		public static const appSize: Point = new Point(760, 1135);
		public static const viewSize: Point = new Point(760, 1300);
		
		public function duscussion_ok() {
			Security.allowDomain('*');
			super(appSize.x, appSize.y, 6);
			okInit();
		}
		
		override protected function getStyles(): uif_styles {
			return new clt_styles();
		}
		
		public function get flashVars(): Object {
			return Object( LoaderInfo(stage.loaderInfo ).parameters );			
		}
		
		override protected function createExternalServer(): api_server {
			return new clt_okserver(this, flashVars);
		}
		
		override protected function initialize(): void {
			stage.scaleMode 	= StageScaleMode.NO_SCALE;
			stage.align 		= StageAlign.TOP_LEFT;
		}
		
		protected function superInitialize(): void {
			super.initialize();
			Odnoklassniki.callRestApi("discussions.getDiscussions", function(a_data: Object): void {
				trace(a_data);
				
				var _list: clt_list = new clt_list(layers[0], 0, 0, 400, 500, a_data.discussions, function(item: Object): void {
					Odnoklassniki.callRestApi("photos.getPhotoInfo", function(a_data: Object): void {
						
					}, Odnoklassniki.getSendObject({
						uid: item['parentEntityId'], 
						photo_id: item['entityId']
					}));
/*					
					Discussions.getDiscussionComments(item.entityType, item.entityId, function(i_data: Object): void {
					});
					*/
				}, function(item: Object): String {
					return item['lastUserAccessDate'] + ' ' + item['subjectLabel'];
				});
			}, Odnoklassniki.getSendObject({
				entityTypes: 'USER_PHOTO', 
				offset: 0 , 
				count: 50
			}));
		}
		
		protected function okInit(): void {
			Odnoklassniki.initialize(this, SECRETS[flashVars.api_id]);
			Odnoklassniki.addEventListener(ApiServerEvent.CONNECTED, onConnect);
			Odnoklassniki.addEventListener(ApiServerEvent.CONNECTION_ERROR, onErrorConnection);
			Odnoklassniki.addEventListener(ApiServerEvent.PROXY_NOT_RESPONDING, onErrorConnection);
			Odnoklassniki.addEventListener(ApiServerEvent.NOT_YET_CONNECTED, onErrorConnection);
			Odnoklassniki.addEventListener(ApiCallbackEvent.CALL_BACK, doCallBack);
		}
		
		protected function updateScaleFromStage():void {
			scale = stage.stageWidth / width;
			superInitialize(); 
			Odnoklassniki.setWindowSize(viewSize.x, viewSize.y);
		}
		
		protected function onConnect(event : ApiServerEvent) : void {
			updateScaleFromStage();
		}
		
		protected function onErrorConnection(event : ApiServerEvent) : void {
		}
		
		protected function doCallBack(event: ApiCallbackEvent): void {
		}
	}
}